package com.vcredit.creditcat.manager.admin;

import com.vcredit.creditcat.api.IChannelExternalManager;
import com.vcredit.creditcat.model.ChannelStatisticsExternal;
import com.vcredit.creditcat.service.ChannelStatisticsExternalService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by liaoshengjun on 2017/11/14.
 */
public class ChannelExternalManager implements IChannelExternalManager{
	@Autowired
	private ChannelStatisticsExternalService channelStatisticsExternalService;

	private static Logger logger = LoggerFactory.getLogger(ChannelExternalManager.class);
	@Override
	public void excute() throws Exception {
		logger.info("开始产品统计自动生成！！");
		try {
			ChannelStatisticsExternal paramObj=new ChannelStatisticsExternal();
			Calendar calendar=Calendar.getInstance();
			calendar.add(Calendar.DAY_OF_YEAR,-1);
			Date date=calendar.getTime();
			String yesterday=new SimpleDateFormat("yyyy-MM-dd").format(date);
			paramObj.setStatisticalStartTime(yesterday);
			List<ChannelStatisticsExternal> list=channelStatisticsExternalService.find(paramObj);
			//如果当天已有数据则不插入数据
			if(list!=null && list.size()>0)return;
			channelStatisticsExternalService.insertData();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("产品统计自动生成失败~！", e.getMessage());
		}
	}
}
